/**++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++**
 *  @author     Andrew Wei
 *  @portfolio  http://andrewwei.mu
 *
 *  ©2012 Andrew Wei
 *
 *  This software is released under the MIT License:
 *  http://www.opensource.org/licenses/mit-license.php
 **++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++**/
package com.ghoztudios.display
{
    import flash.display.DisplayObject;
    import flash.display.MovieClip;
    import flash.display.Shape;
    import flash.display.Sprite;
    import flash.filters.BitmapFilter;
    import flash.geom.ColorTransform;
    import flash.text.TextFormat;

    /**
     * NavigationButton inherits SwitchOnStage and serves as a custom template
     * for buttons to work with DropdownNavigationSystem.
     *
     * @langversion     3.0
     * @playerversion   Flash 10.3.183.20
     */
    public class NavigationButton extends ButtonOnStage
    {
        private static const ID:String = 'com.ghoztudios.display.NavigationButton';


        // Constants
        /**
         * Button orientations.
         *
         * @langversion     3.0
         * @playerversion   Flash 10.3.183.20
         */
        private static const TOP_ORIENTATED:String    = 'top';
        private static const RIGHT_ORIENTATED:String  = 'right';
        private static const BOTTOM_ORIENTATED:String = 'bottom';
        private static const LEFT_ORIENTED:String     = 'left';

        /**
         * Button text vertical align properties.
         *
         * @langversion     3.0
         * @playerversion   Flash 10.3.183.20
         */
        public static const VERTICAL_ALIGN_TOP:String    = 'top';
        public static const VERTICAL_ALIGN_MIDDLE:String = 'middle';
        public static const VERTICAL_ALIGN_BOTTOM:String = 'bottom';


        // Private Properties
        /**
         * Width of button.
         *
         * @langversion     3.0
         * @playerversion   Flash 10.3.183.20
         */
        private var _w:Number = NaN;

        /**
         * Height of button.
         *
         * @langversion     3.0
         * @playerversion   Flash 10.3.183.20
         */
        private var _h:Number = NaN;


        // Stage Instances
        /**
         * Indicator DisplayObject.
         *
         * @langversion     3.0
         * @playerversion   Flash 10.3.183.20
         */
        private static const INSTANCE_INDICATOR:String = 'insIndicator';
        protected var _indicator:DisplayObject  = null;

        /**
         * Background DisplayObject.
         *
         * @langversion     3.0
         * @playerversion   Flash 10.3.183.20
         */
        private static const INSTANCE_BACKGROUND:String = 'insBackground';
        protected var _background:DisplayObject = null;


        // Initialization
        /**
         * Creates a new NavigationButton instance.
         *
         * @langversion     3.0
         * @playerversion   Flash 10.3.183.20
         */
        public function NavigationButton():void {}


        // Overridden Methods
        /**
         * Protected init function.
         *
         * @langversion     3.0
         * @playerversion   Flash 10.3.183.20
         */
        override protected function _init():void
        {
            super._init();

            // init stage references
            _indicator = getChildByName(INSTANCE_INDICATOR) as DisplayObject;
            _background = getChildByName(INSTANCE_BACKGROUND) as DisplayObject;

            // sanity check, reset all component positions to origin
            _indicator.x = 0;
            _indicator.y = 0;

            if (_label != null)
            {
                _label.x = 0;
                _label.y = 0;
            }

            _w = width;
            _h = height;
        }

        /**
         * Protected destroy function.
         *
         * @langversion     3.0
         * @playerversion   Flash 10.3.183.20
         */
        override protected function _destroy():void
        {

        }


        // Public Methods
        /**
         * Public init function.
         *
         * @param   $width
         * @param   $height
         * @param   $orientation
         * @param   $label
         * @param   $textFormat
         * @param   $verticalAlign
         * @langversion     3.0
         * @playerversion   Flash 10.3.183.20
         */
        public function init($width:Number, $height:Number, $orientation:String, $label:String, $textFormat:TextFormat = null, $verticalAlign:String = TOP_ORIENTATED):void
        {
            setWidth($width);
            setHeight($height);
            setIndicator($orientation);
            setLabel($label, $textFormat);

            if (_label == null)
            {
                throw new Error('No labels found inside SwitchOnStage instance.');
                return;
            }

            switch ($verticalAlign)
            {
                case VERTICAL_ALIGN_TOP:
                    _label.y = 0;
                    break;
                case VERTICAL_ALIGN_MIDDLE:
                    _label.y = (height - _label.height) / 2;
                    break;
                case VERTICAL_ALIGN_BOTTOM:
                    _label.y = height - _label.height;
                    break;
                default:
                    // do nothing
            }
        }

        /**
         * Sets the indicator position according to the orientation of the dropdown menu system.
         * The current behavior of com.ghoztudios.display.NavigationButton has 4 different orientations
         * of the indicator. It can either be top, right, bottom, or left (that is, if _indicator
         * is defined.
         *
         * @param   $orientation
         * @langversion     3.0
         * @playerversion   Flash 10.3.183.20
         */
        public function setIndicator($orientation:String = BOTTOM_ORIENTATED):void
        {
            switch ($orientation)
            {
                case TOP_ORIENTATED:
                    _indicator.visible = true;
                    _indicator.x = (_w - _indicator.width) / 2;
                    _indicator.y = 0;
                    break;
                case RIGHT_ORIENTATED:
                    _indicator.visible = true;
                    _indicator.x = _w - _indicator.width;
                    _indicator.y = (_h - _indicator.height) / 2;
                    break;
                case BOTTOM_ORIENTATED:
                    _indicator.visible = true;
                    _indicator.x = (_w - _indicator.width) / 2;
                    _indicator.y = _h - _indicator.height;
                    break;
                case LEFT_ORIENTED:
                    _indicator.visible = true;
                    _indicator.x = 0;
                    _indicator.y = (_h - _indicator.height) / 2;
                    break;
                case null:
                    _indicator.visible = false;
                    break;
                default:
                    // do nothing
            }
        }

        /**
         * Sets the width of the button and the text label if it exists.
         *
         * @param   $width
         * @langversion     3.0
         * @playerversion   Flash 10.3.183.20
         */
        public function setWidth($width:Number):void
        {
            var target:DisplayObject = (_background == null) ? _highlight : _background;

            _w = $width;

            target.width = _w;
            _label.width = _w;
        }

        /**
         * Sets the height of the button and the text label if it exists.
         *
         * @param   $height
         * @langversion     3.0
         * @playerversion   Flash 10.3.183.20
         */
        public function setHeight($height:Number):void
        {
            var target:DisplayObject = (_background == null) ? _highlight : _background;

            _h = $height;

            target.height = _h;
            _label.height = _h;
        }

        /**
         * Sets background color.
         *
         * @param   $color
         * @langversion     3.0
         * @playerversion   Flash 10.3.183.20
         */
        public function setBackgroundColor($color:uint):void
        {
            var background:DisplayObject = (_background == null) ? _highlight : _background;
            var ct:ColorTransform    = background.transform.colorTransform;

            ct.color = $color;
            background.transform.colorTransform = ct;

            if (background == _highlight)
                _baseTintColor = $color;
        }
    }
}
